import DrawPoint from './DrawChildren/DrawPoint';
import DrawPolyline from './DrawChildren/DrawPolyline';
import DrawPolygon from './DrawChildren/DrawPolygon';
import DrawRectangle from './DrawChildren/DrawRectangle';
import DrawCircle from './DrawChildren/DrawCircle';
import DrawPointHeight from './DrawChildren/DrawPointHeight';

// 该type - class 映射关系必须与  this.InstanceManager 一致
const TYPE_CLASS = {
    Point: DrawPoint,
    Polyline: DrawPolyline,
    Polygon: DrawPolygon,
    Rectangle: DrawRectangle,
    Circle: DrawCircle,
    PointHeight: DrawPointHeight,
};

export default class CesiumDraw {
    constructor(viewer, globalConfig) {
        this.viewer = viewer;
        /** 全局配置项 */
        this.globalConfig = globalConfig;
        /** 实例化数组 */
        this.InstanceManager = {
            Point: null,
            Polyline: null,
            Polygon: null,
            Rectangle: null,
            Circle: null,
            PointHeight: null,
        };
    }
    // 效验参数
    vilidate(type) {
        const typeList = Object.keys(this.InstanceManager);
        const flag = typeList.includes(type);
        if (!flag) {
            console.error(`参数错误: CesiumDraw type 错误，必须为：${typeList.join(',')}`);
        }
        return flag;
    }
    // 获取绘制实例
    getDrawInstanceObj(type) {
        if (!this.vilidate(type)) return;
        if (!this.InstanceManager[type]) {
            this.InstanceManager[type] = new TYPE_CLASS[type](this.viewer, this.globalConfig);
        }
        return this.InstanceManager[type];
    }
}

// 静态方法
CesiumDraw.getType = function () {
    const typeList = Object.keys(TYPE_CLASS);
    console.log('CesiumDraw type 参数必须为', typeList.join('、'));
};

/**
 * 开始绘制
 * @param {*} type
 * @param {* 配置项 } config
 */
CesiumDraw.prototype.startDrawPromise = function (type, config) {
    const instance = this.getDrawInstanceObj(type);
    return instance.startDrawPromise(config); // 开始绘制
};
/**
 * 结束绘制
 * @param {*} type
 */
CesiumDraw.prototype.endDrawPromise = function (type) {
    const instance = this.getDrawInstanceObj(type);
    return instance.endDrawPromise(); // 结束绘制
};
/**
 * 开始绘制
 * @param {*} type
 * @param {* 配置项 } config
 */
CesiumDraw.prototype.startDraw = function (type, config) {
    const instance = this.getDrawInstanceObj(type);
    instance.startDraw(config); // 开始绘制
};
/**
 * 结束绘制
 * @param {*} type
 */
CesiumDraw.prototype.endDraw = function (type) {
    const instance = this.getDrawInstanceObj(type);
    instance.endDraw(); // 结束绘制
};
/**
 * 清空 type 类型绘制
 * @param {*} type
 */
CesiumDraw.prototype.removeDraw = function (type) {
    const instance = this.getDrawInstanceObj(type);
    instance.removeDraw(); // 结束绘制
};
/**
 * 清空所有绘制
 * 传入type 则删除对应类型的， 否则清空所有
 */
CesiumDraw.prototype.removeAllDraw = function (type) {
    if (type) {
        const instance = this.getDrawInstanceObj(type);
        instance.removeAllDraw(); // 结束绘制
    } else {
        for (let key in this.InstanceManager) {
            const instance = this.InstanceManager[key];
            if (instance) instance.removeAllDraw();
        }
    }
};
/**
 * 获取当次绘制所有实体
 * @param {} type
 * @returns
 */
CesiumDraw.prototype.getDraw = function (type) {
    const instance = this.getDrawInstanceObj(type);
    const entityList = instance.getDraw(); // 结束绘制
    return entityList;
};
/**
 * 获取全部实体
 * @param {*} type
 * @returns
 */
CesiumDraw.prototype.getAllDraw = function (type) {
    const instance = this.getDrawInstanceObj(type);
    const entityList = instance.getAllDraw(); // 结束绘制
    return entityList;
};
